package org.snare.alumni.modules.admin.activity.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.snare.alumni.modules.admin.activity.dto.ActivityDTO;
import org.snare.alumni.modules.admin.activity.dto.ActivityUserDTO;
import org.snare.alumni.modules.admin.activity.entity.ActivityEntity;
import org.snare.alumni.modules.admin.activity.entity.ActivityUserEntity;
import org.snare.alumni.modules.admin.activity.mapper.ActivityMapper;
import org.snare.alumni.modules.admin.activity.mapper.ActivityUserMapper;
import org.snare.alumni.modules.admin.activity.service.api.ActivityService;
import org.snare.alumni.modules.admin.card.entity.CardEntity;
import org.snare.spider.cobweb.crm.user.entity.CrmUserEntity;
import org.snare.spider.common.base.Limit;
import org.snare.spider.common.exception.BusinessException;
import org.snare.spider.common.utils.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
@RequiredArgsConstructor
public class ActivityServiceImpl implements ActivityService {

    private final ActivityMapper activityMapper;

    private final ActivityUserMapper activityUserMapper;

    @Override
    public IPage<ActivityDTO> page(ActivityDTO params, Limit limit) {
        IPage<ActivityEntity> page = new LambdaQueryChainWrapper<>(activityMapper)
                .eq(StringUtils.isNotBlank(params.getTitle()), ActivityEntity::getTitle, params.getTitle())
                .page(limit.page());
        return BeanUtils.copy(page, ActivityDTO.class);
    }

    @Override
    public List<ActivityDTO> list(ActivityDTO params) {
        MPJLambdaWrapper<ActivityEntity> wrapper = new MPJLambdaWrapper<>();
//        wrapper.selectAll()
        return activityMapper.selectJoinList(ActivityDTO.class, wrapper);
    }

    @Override
    public void add(ActivityDTO params) {
        ActivityEntity entity = BeanUtils.copy(params, ActivityEntity.class);
        activityMapper.insert(entity);
    }

    @Override
    public void edit(ActivityDTO params) {
        ActivityEntity entity = BeanUtils.copy(params, ActivityEntity.class);
        activityMapper.updateById(entity);
    }

    @Override
    public ActivityDTO detail(String id) {
        return BeanUtils.copy(activityMapper.selectById(id), ActivityDTO.class);
    }

    @Override
    public void delete(String id) {
        activityMapper.deleteById(id);
    }

    @Override
    public void signUp(String id, String uid) {
        ActivityEntity activityEntity = activityMapper.selectById(id);
        if (activityEntity.getStatus() == 2) {
            throw new RuntimeException("活动已结束");
        }

        Long count = new LambdaQueryChainWrapper<>(activityUserMapper)
                .eq(ActivityUserEntity::getActivityId, id)
                .eq(ActivityUserEntity::getUid, uid)
                .count();
        if (count > 0) {
            throw new RuntimeException("已报名");
        }
        ActivityUserEntity entity = new ActivityUserEntity();
        entity.setActivityId(id);
        entity.setUid(uid);
        entity.setStatus("0");
        activityUserMapper.insert(entity);
    }

    @Override
    public IPage<ActivityUserDTO> users(String id, String status, String name, Limit limit) {
        MPJLambdaWrapper<ActivityUserEntity> wrapper = new MPJLambdaWrapper<>();
        wrapper.select(CrmUserEntity::getRealName)
                .select(CardEntity::getPhone)
                .selectAll(ActivityUserEntity.class)
                .leftJoin(CardEntity.class, CardEntity::getUid, ActivityUserEntity::getUid)
                .leftJoin(CrmUserEntity.class, CrmUserEntity::getId, CardEntity::getUid)
                .eq(ActivityUserEntity::getActivityId, id)
                .eq(StringUtils.isNotBlank(status), ActivityUserEntity::getStatus, status)
                .eq(StringUtils.isNotBlank(name), CrmUserEntity::getRealName, name);
        return activityUserMapper.selectJoinPage(limit.page(), ActivityUserDTO.class, wrapper);
    }

    @Override
    public void approve(Map<String, Object> params) {
        if (params.get("id") == null || params.get("status") == null) {
            throw new BusinessException("参数校验错误");
        }
        new LambdaUpdateChainWrapper<>(activityUserMapper)
                .set(ActivityUserEntity::getStatus, params.get("status"))
                .eq(ActivityUserEntity::getId, params.get("id"))
                .update();
    }


}
